package com.group.project.hrms.service.impl;

import com.group.project.hrms.entity.DingLeaveRecord;
import com.group.project.hrms.mapper.DingLeaveRecordMapper;
import com.group.project.hrms.service.IDingLeaveRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class DingLeaveRecordService implements IDingLeaveRecordService {

    @Autowired
    private DingLeaveRecordMapper dingLeaveRecordMapper;

    @Override
    public int insertDingLeaveRecord(DingLeaveRecord dingLeaveRecord) {
        return dingLeaveRecordMapper.insert(dingLeaveRecord);
    }

    @Override
    public int deleteDingLeaveRecord(DingLeaveRecord dingLeaveRecord) {
        return dingLeaveRecordMapper.deleteByPrimaryKey(dingLeaveRecord);
    }

    @Override
    public int updateDingLeaveRecord(DingLeaveRecord dingLeaveRecord) {
        return dingLeaveRecordMapper.updateByPrimaryKey(dingLeaveRecord);
    }

    @Override
    public DingLeaveRecord selectOneDingLeaveRecord(DingLeaveRecord dingLeaveRecord) {
        Example example = new Example(DingLeaveRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("startTime", dingLeaveRecord.getStartTime());
        criteria.andEqualTo("durationPercent", dingLeaveRecord.getDurationPercent());
        criteria.andEqualTo("endTime", dingLeaveRecord.getEndTime());
        criteria.andEqualTo("leaveCode", dingLeaveRecord.getLeaveCode());
        criteria.andEqualTo("durationUnit", dingLeaveRecord.getDurationUnit());
        criteria.andEqualTo("userid", dingLeaveRecord.getUserid());
        return dingLeaveRecordMapper.selectOneByExample(example);
    }

    @Override
    public List<DingLeaveRecord> selectDingLeaveRecordByRangeDate(String startTime, String endTime) {
        Example example = new Example(DingLeaveRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orBetween("startTime", startTime, endTime);
        criteria.orBetween("endTime", startTime, endTime);
        return dingLeaveRecordMapper.selectByExample(example);
    }

    @Override
    public List<DingLeaveRecord> selectDingLeaveRecordByMonth(String month) {
        Example example = new Example(DingLeaveRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orLike("startTime", "%" + month + "%");
        criteria.orLike("endTime", "%" + month + "%");
        return dingLeaveRecordMapper.selectByExample(example);
    }

    @Override
    public List<DingLeaveRecord> selectAllDingLeaveRecord() {
        return dingLeaveRecordMapper.selectAll();
    }
}
